<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-card class="apiProperties">
  <mat-card-content>
    <div class="apiProperties__header">
      <div class="apiProperties__header__text">
        <h2>API properties</h2>
        These properties are automatically injected into the expression language context to be used later.<br />
        For example, to get the value of the property <code>my-property</code> defined in API properties:
        <code [innerHTML]="'{#properties[\'my-property\']}'"> </code>
      </div>
      <div *ngIf="!isReadOnly" class="apiProperties__header__btn">
        <button
          *gioPermission="{ anyOf: ['api-definition-u'] }"
          mat-raised-button
          type="button"
          (click)="importProperties()"
          aria-label="Import properties"
        >
          Import
        </button>
        <a
          *gioPermission="{ anyOf: ['api-definition-u'] }"
          mat-raised-button
          type="button"
          aria-label="Manage dynamically"
          [routerLink]="isV4 ? './v4/dynamic-properties' : './dynamic-properties'"
          >Manage dynamically</a
        >
        <button
          *gioPermission="{ anyOf: ['api-definition-u'] }"
          mat-raised-button
          type="button"
          color="primary"
          (click)="addProperty()"
          aria-label="Add property"
        >
          <mat-icon svgIcon="gio:plus"></mat-icon> Add property
        </button>
      </div>
    </div>
  </mat-card-content>

  <gio-table-wrapper
    class="apiProperties__table"
    [filters]="tableFilters"
    [length]="totalLength"
    (filtersChange)="onFiltersChanged($event)"
  >
    <table mat-table [dataSource]="filteredTableData" [formGroup]="propertiesFormGroup" aria-label="API Properties">
      <ng-containexr matColumnDef="key">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Key</th>
        <td mat-cell *matCellDef="let element" [formGroupName]="element._id">
          <mat-form-field class="apiProperties__table__field">
            <mat-label>Key</mat-label>
            <input matInput formControlName="key" required />
            <mat-error *ngIf="propertiesFormGroup.get(element._id).get('key').hasError('required')">Key is required.</mat-error>
            <mat-error *ngIf="propertiesFormGroup.get(element._id).get('key').hasError('isUnique')">Key already exists.</mat-error>
          </mat-form-field>
        </td>
      </ng-containexr>

      <ng-container matColumnDef="value">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Value</th>
        <td mat-cell *matCellDef="let element" [formGroupName]="element._id">
          <mat-form-field class="apiProperties__table__field">
            <mat-label>Value</mat-label>
            <input matInput formControlName="value" />
          </mat-form-field>
        </td>
      </ng-container>

      <ng-container matColumnDef="characteristic">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>Characteristic</th>
        <td mat-cell *matCellDef="let element" [formGroupName]="element._id">
          <span *ngIf="element.encrypted" class="gio-badge-success">Encrypted</span>
          <span *ngIf="!element.encrypted && element.encryptable" class="gio-badge-accent">Encrypted on save</span>
          <span *ngIf="!element.encrypted && !element.encryptable" class="gio-badge-neutral">Unencrypted </span>
          <span *ngIf="element.dynamic" class="gio-badge-neutral">Dynamic</span>
        </td>
      </ng-container>

      <ng-container matColumnDef="actions">
        <th mat-header-cell *matHeaderCellDef mat-sort-header></th>
        <td mat-cell *matCellDef="let element">
          <ng-container *ngIf="!element.dynamic">
            <button
              *ngIf="element.encrypted || element.encryptable"
              type="button"
              mat-icon-button
              aria-label="Renew encrypted value"
              matTooltip="Renew encrypted value"
              (click)="renewEncryptedPropertyValue(element._id)"
            >
              <mat-icon svgIcon="gio:refresh-cw"></mat-icon>
            </button>

            <button
              *ngIf="!element.encrypted && !element.encryptable"
              type="button"
              mat-icon-button
              aria-label="Encrypt value"
              matTooltip="Encrypt value"
              (click)="encryptPropertyValue(element._id)"
            >
              <mat-icon svgIcon="gio:lock"></mat-icon>
            </button>
          </ng-container>

          <button
            type="button"
            mat-icon-button
            aria-label="Remove property"
            [matTooltip]="element.dynamic ? 'Remove property. Can be re-added with next sync.' : 'Remove property'"
            (click)="removeProperty(element._id)"
          >
            <mat-icon svgIcon="gio:cancel"></mat-icon>
          </button>
        </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>

      <!-- Row shown when there is no data -->
      <tr class="mat-mdc-row mdc-data-table__row" *matNoDataRow>
        <td class="mat-mdc-cell mdc-data-table__cell" [attr.colspan]="displayedColumns.length">
          <div *ngIf="!isLoading" class="mat-body">No properties</div>
          <div *ngIf="isLoading" class="mat-body">Loading...</div>
        </td>
      </tr>
    </table>
  </gio-table-wrapper>
</mat-card>

<gio-save-bar [opened]="isDirty" (submitted)="onSave()" (resetClicked)="onReset()"> </gio-save-bar>
